package com.vaadin.lab7;

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;

import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Component;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class Lab extends Application {

    @Override
    public void init() {
        VerticalLayout mainLayout = new VerticalLayout();
        mainLayout.setSizeFull();
        Window mainWindow = new Window("Lab", mainLayout);
        setMainWindow(mainWindow);
        final Item item = createItem();

        PopupView popup = new PopupView(new PopupView.Content() {

            @Override
            public Component getPopupComponent() {
                final Table table = new Table();
                table.addContainerProperty("id", String.class, null);
                table.addContainerProperty("value", String.class, null);
                int i = 0;
                for (Object propertyId : item.getItemPropertyIds()) {
                    table.addItem(new Object[] { propertyId.toString(),
                            item.getItemProperty(propertyId).getValue() }, i++);
                }
                table.setPageLength(5);

                return table;
            }

            @Override
            public String getMinimizedValueAsHTML() {
                return "Show Item";
            }
        });

        mainLayout.addComponent(popup);
        mainLayout.setExpandRatio(popup, 1.0f);
    }

    private static Item createItem() {
        Product product = new Product();
        product.setName("");
        product.setOptions(new HashSet<String>(Arrays.asList("First")));
        product.setAvailable(Calendar.getInstance().getTime());
        return new BeanItem<Product>(product);
    }
}
